package by.yakusekmaxim.bookgid.webapp.page.admin.genre;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.datamodel.Genre;
import by.yakusekmaxim.bookgid.services.GenreService;
import by.yakusekmaxim.bookgid.webapp.page.AdminLayout;

public class EditGenrePage extends AdminLayout {
	
	@Inject
	private GenreService genreService;
	
	public EditGenrePage(final Genre genre) {
		Form<Genre> form = new Form<Genre>("form", new CompoundPropertyModel<Genre>(genre));

		final Label tfId = new Label("id", genre.getId());
		form.add(tfId);
		
		final TextField<String> tfName = new TextField<String>("name");
		tfName.add(new PropertyValidator<String>());
		form.add(tfName);
		
		form.add(new Link<Object>("cancel") {
			@Override
			public void onClick() {
				setResponsePage(new GenrePage());
			}
		});
		
		form.add(new SubmitLink("save") {
			@Override
			public void onSubmit() {
				super.onSubmit();
				genreService.saveOrUpdate(genre);

				GenrePage page = new GenrePage();

//				page.error("Custom error");
				page.info("Жанр:" + genre.getName() + " сохранён!");
//				page.warn("Custom warn");

				setResponsePage(page);
			}

			@Override
			public void onError() {

				super.onError();
			}
		});

		add(form);
	}

	protected void onInitialize() {
		super.onInitialize();
	}

	@Override
	protected WebMarkupContainer getTarget() {
		return targetGenre;
	}

	@Override
	protected IModel<String> getPageTitle() {
		return new ResourceModel("adminmenu.newgenre");
	}

}
